// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Platform.java

package com.sgame.android;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.view.KeyEvent;
import android.widget.Toast;
import java.io.IOException;
import java.util.*;
import com.sgame.android.social.Service;;
// Referenced classes of package com.mobage.android:
//            Mobage


public class Platform
{

    public static interface Regional
    {

        public abstract void restart();

        public abstract void stop();

        public abstract void launchPortalApp(Service.OnDialogComplete ondialogcomplete)
            throws SDKException;

        public abstract void openUserProfile(String s, Service.OnDialogComplete ondialogcomplete)
            throws SDKException;

        public abstract void purchaseItem(Mobage.MarketCode marketcode, String s, String s1, String s2, String s3, Service.OnDialogComplete ondialogcomplete);

        public abstract void onBillingSupported(boolean flag);
    }


    public Platform()
    {
        mMarketCode = Mobage.MarketCode.MOBAGE;
        mOnPause = true;
    }

    public static Platform getInstance()
    {
        if(sInstance == null)
            sInstance = new Platform();
        return sInstance;
    }

    public void registerResources(int splashLayoutId, int closeButtonId, int coinBlueId, int coinWhiteId)
    {
        mSplashLayoutId = splashLayoutId;
        mCloseButtonId = closeButtonId;
        mCoinBlueId = coinBlueId;
        mCoinWhiteId = coinWhiteId;
    }

    public int getSplashLayoutId()
    {
        return mSplashLayoutId;
    }

    public int getCloseButtonId()
    {
        return mCloseButtonId;
    }

    public int getCoinBlueId()
    {
        return mCoinBlueId;
    }

    public int getCoinWhiteId()
    {
        return mCoinWhiteId;
    }

    public void initialize(Activity activity, String appId, Mobage.Region region, ServerMode serverMode, String consumerKey, String consumerSecret)
    {
        mAppId = appId;
        calcDeviceId(activity);
        calcCarrier(activity);
        calcAppVersion(activity);
        mListeners = new HashMap();
        checkResourceFilesExistence();
        try
        {
            Mobage.getInstance().initialize(activity, serverMode != ServerMode.PRODUCTION ? ServerMode.SANDBOX : ServerMode.PRODUCTION, appId, (new Integer(mAppVersion)).toString(), consumerKey, consumerSecret);
        }
        catch(Exception e)
        {
            MLog.e("Platform", "com.mobage.ww.android.Mobage.getInstance().initialize:", e);
        }
        mIsReady = true;
    }

    private static void checkResourceFilesExistence()
    {
        try
        {
            Mobage.currentActivity.getResources().getAssets().open("mobage/us/images/mobagelogotm.png");
        }
        catch(IOException e)
        {
            Toast.makeText(Mobage.currentActivity, "resource files for US is missing! ", 1).show();
        }
    }

    public void setShouldSkipNextReport(boolean enabled)
    {
        throw new RuntimeException("Not implemented!");
    }

    public void setReady(boolean ready)
    {
        mIsReady = ready;
    }

    public boolean isReady()
    {
        return mIsReady;
    }

    public String getAppId()
    {
        return mAppId;
    }

    public String getDeviceId()
    {
        return mDeviceId;
    }

    public void calcDeviceId(Activity activity)
    {
        TelephonyManager tMgr = (TelephonyManager)activity.getSystemService("phone");
        String telephonyId = tMgr.getDeviceId();
        if(telephonyId != null)
        {
            mDeviceId = telephonyId;
            return;
        }
        WifiManager wMgr = (WifiManager)activity.getSystemService("wifi");
        String wifiId = wMgr.getConnectionInfo().getMacAddress();
        if(wifiId != null)
        {
            mDeviceId = wifiId;
            return;
        } else
        {
            MLog.w("Platform", "device has no Telephony or Wifi id");
            mDeviceId = "DEAD-BEEF";
            return;
        }
    }

    public String getCarrier()
    {
        return mCarrier;
    }

    public void calcCarrier(Activity activity)
    {
        String carrier = ((TelephonyManager)activity.getSystemService("phone")).getNetworkOperatorName();
        if(carrier != null && carrier.length() > 0)
            mCarrier = carrier;
        else
            mCarrier = "unknown";
    }

    public String getSdkVersion()
    {
        return "1.0";
    }

    public int getAppVersion()
    {
        return mAppVersion;
    }

    public String getAppVersionName()
    {
        return mAppVersionName;
    }

    public void calcAppVersion(Activity activity)
    {
        String packageName = activity.getPackageName();
        MLog.d("Platform", (new StringBuilder()).append("calcAppVersion:Application's package name=").append(packageName).toString());
        try
        {
            PackageInfo packageInfo = activity.getPackageManager().getPackageInfo(packageName, 128);
            mAppVersion = packageInfo.versionCode;
            mAppVersionName = packageInfo.versionName;
        }
        catch(android.content.pm.PackageManager.NameNotFoundException e)
        {
            MLog.e("Platform", (new StringBuilder()).append("AppVersion error:").append(packageName).toString(), e);
        }
        MLog.d("Platform", (new StringBuilder()).append("calcAppVersion:Application version=").append(mAppVersion).toString());
    }

    public void addPlatformListener(Mobage.PlatformListener listener)
    {
        if(listener == null)
        {
            throw new IllegalArgumentException("listener is null.");
        } else
        {
            mListeners.put(listener, "");
            return;
        }
    }

    public void removePlatformListener(Mobage.PlatformListener listener)
    {
        mListeners.remove(listener);
    }

    public void onSplashComplete()
    {
        for(Iterator ite = mListeners.keySet().iterator(); ite.hasNext(); ((Mobage.PlatformListener)ite.next()).onSplashComplete());
    }

    public boolean isJP()
    {
        return false;
    }

    /**
     * @deprecated Method isUS is deprecated
     */

    public boolean isUS()
    {
        return true;
    }

    public void setUserId(String userId)
    {
        mUserId = userId;
    }

    public String getUserId()
    {
        return mUserId;
    }

    public void launchPortalApp(Service.OnDialogComplete onComplete)
        throws SDKException
    {
        mRegional.launchPortalApp(onComplete);
    }

    public void openUserProfile(String userId, Service.OnDialogComplete onComplete)
        throws SDKException
    {
        mRegional.openUserProfile(userId, onComplete);
    }

    public Mobage.MarketCode getMarketCode()
    {
        return mMarketCode;
    }

    public void calcMarketCode(Activity activity)
    {
        MLog.d("Platform", (new StringBuilder()).append("calcMarketCode:Application's market code=").append(mMarketCode).toString());
    }

    public void purchaseItem(String orderId, String productId, String type, String unitPrice, Service.OnDialogComplete onComplete)
    {
        mRegional.purchaseItem(getMarketCode(), orderId, productId, type, unitPrice, onComplete);
    }

    public void pause()
    {
        mOnPause = true;
		Mobage.onPause();
    }

    public void resume()
    {
        mOnPause = false;
		Mobage.onResume();
    }

    public void stop()
    {
		Mobage.onStop();
    }

    public void destroy()
    {
    }

    public void restart()
    {
		Mobage.onRestart();
    }


    public void registerTick()
    {
    }

    public void onBillingSupported(boolean supported)
    {
        if(mRegional != null)
            mRegional.onBillingSupported(supported);
    }

    public static boolean dispatchKeyEventDeviceDependPatch(KeyEvent event)
    {
        String modelName = Build.MODEL;
        if(modelName.equals("SC-03D") && event.getKeyCode() == 82 && (event.getFlags() & 0x80) != 0)
        {
            MLog.d("Platform", (new StringBuilder()).append(modelName).append(":Suppressing LONG PRESS MENU key event.").toString());
            return true;
        } else
        {
            return false;
        }
    }

    public HashMap getListeners()
    {
        return mListeners;
    }

    public boolean onBackPressed()
    {
        return false;
    }

    private static final String TAG = "Platform";
    private static final String SDK_VERSION = "1.0";
    private static Platform sInstance = null;
    private Regional mRegional;
    private int mSplashLayoutId;
    private int mCloseButtonId;
    private int mCoinBlueId;
    private int mCoinWhiteId;
    private String mAppId;
    private String mDeviceId;
    private String mCarrier;
    private int mAppVersion;
    private String mAppVersionName;
    private Mobage.MarketCode mMarketCode;
    private boolean mIsReady;
    HashMap mListeners;
    private String mUserId;
    boolean mOnPause;

}
